<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商管理系统</span>
      </div>
      <el-button type="warning" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="tM?'64px':'200px'">
        <div class="toggle-btn" @click="toggleMenu">|||</div>
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :router="true"
          :unique-opened="true"
          :collapse="tM"
          :collapse-transition="false"
          :default-active="defaultActive"
        >
          <el-submenu :index="menu.id+''" v-for="menu in menuList" :key="menu.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{menu.authName}}</span>
            </template>
            <el-menu-item
              @click="activeItem(subMenu.path)"
              :index="'/'+subMenu.path"
              v-for="subMenu in menu.children"
              :key="subMenu.id"
            >
              <i class="el-icon-menu"></i>
              <span>{{subMenu.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边内容 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      tM: false,
      defaultActive: ""
    };
  },
  created() {
    this.getMenuList();
    this.defaultActive = sessionStorage.getItem("activePath");
  },
  methods: {
    //退出
    logout() {
      //清楚token
      sessionStorage.clear();
      //跳转登录页面
      this.$router.push("/login");
    },
    //获取菜单列表 使用async 和 await解构返回的参数
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus");
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    //导航栏折叠
    toggleMenu() {
      this.tM = !this.tM;
    },
    //点击高亮显示
    activeItem(path) {
      //存储到sessionStroage中
      sessionStorage.setItem("activePath", "/" + path);
      this.defaultActive = "/" + path;
    }
  }
};
</script>
<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  padding-left: 0;
}
.el-header div {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-header div span {
  margin-left: 15px;
}
.el-aside {
  background: #333744;
}
.el-main {
  background: #eaedf1;
}
.el-menu {
  border-right: 0;
}
.toggle-btn {
  background: #666;
  color: #fff;
  font-size: 14px;
  letter-spacing: 2px;
  text-align: center;
  line-height: 24px;
  cursor: pointer;
}
</style>